package com.finger.gulimall.order.listener;

import java.io.IOException;
import com.finger.common.constant.MqOrderConstant;
import com.finger.gulimall.order.entity.to.OrderTo;
import com.finger.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName OrderCloseListener
 * @Description
 * @Author finger
 * @Date 2023/4/29 19:54
 * @Version 1.0
 */
@Service
@RabbitListener(queues = MqOrderConstant.ORDER_RELEASE_ORDER_QUEUE)
public class OrderCloseListener {

	@Autowired
	OrderService orderService;

	@RabbitHandler
	public void closeOrder(OrderTo orderTo, Message message, Channel channel) throws IOException {
		try {
			orderService.closeOrder(orderTo);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (Exception e) {
			e.printStackTrace();
			channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
		}
	}
}
